package comp9417;

import java.util.Iterator;

/**
 * Created by huy on 1/06/14.
 */
public class FoldIterator implements Iterator<Fold> {
    private final int k;
    private final Dataset dataset;
    private int f = 0;
    private final int sizePerFold;

    public FoldIterator(Dataset dataset, int k) {
        this.k = k;
        this.dataset = dataset;
        sizePerFold = dataset.getDatapoints().size() / k;
    }

    @Override
    public boolean hasNext() {
        return f < k - 1;
    }

    @Override
    public Fold next() {
        Dataset test = new Dataset(dataset.getAttributes());
        Dataset training = new Dataset(dataset.getAttributes());
        for (int index = f * sizePerFold; index < f * sizePerFold + sizePerFold; index++) {
            test.addPoint(dataset.getDatapoints().get(index));
        }
        for (int index = 0; index < f * sizePerFold; index++) {
            training.addPoint(dataset.getDatapoints().get(index));
        }
        for (int index = f * sizePerFold + sizePerFold; index < dataset.getDatapoints().size(); index++) {
            training.addPoint(dataset.getDatapoints().get(index));
        }
        f++;
        return new Fold(test, training);
    }

    @Override
    public void remove() {
        throw new UnsupportedOperationException("Not supported");
    }
}
